package com.jnxx.dossier.controller;

import com.alibaba.fastjson.JSONObject;
import com.jnxx.dossier.domain.Dossier;
import com.jnxx.dossier.service.IDossierService;
import com.jnxx.task.WxUtils.TokenUtils;
import com.jnxx.task.domain.OkrStaff;
import com.jnxx.task.service.impl.OkrStaffServiceImpl;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户档案Controller
 * 
 * @author ruoyi
 * @date 2024-01-30
 */

@RestController
@RequestMapping("/api/dossier")
public class DossierController extends BaseController
{
    @Autowired
    private IDossierService dossierService;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private OkrStaffServiceImpl staffService;

    /**
     * 查询客户档案列表
     */
    @PreAuthorize("@ss.hasPermi('api:dossier:list')")
    @GetMapping("/list")
    public TableDataInfo list(Dossier dossier)
    {
        startPage();
        List<Dossier> list = dossierService.selectDossierList(dossier);
        return getDataTable(list);
    }

    /**
     * 导出客户档案列表
     */
    @PreAuthorize("@ss.hasPermi('api:dossier:export')")
    @Log(title = "客户档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Dossier dossier)
    {
        List<Dossier> list = dossierService.selectDossierList(dossier);
        ExcelUtil<Dossier> util = new ExcelUtil<Dossier>(Dossier.class);
        util.exportExcel(response, list, "客户档案数据");
    }

    /**
     * 获取客户档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:dossier:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dossierService.selectDossierById(id));
    }

    /**
     * 新增客户档案
     */
    @PreAuthorize("@ss.hasPermi('api:dossier:add')")
    @Log(title = "客户档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dossier dossier)
    {
        return toAjax(dossierService.insertDossier(dossier));
    }

    /**
     * 修改客户档案
     */
    @PreAuthorize("@ss.hasPermi('api:dossier:edit')")
    @Log(title = "客户档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dossier dossier)
    {
        return toAjax(dossierService.updateDossier(dossier));
    }

    /**
     * 删除客户档案
     */
    @PreAuthorize("@ss.hasPermi('api:dossier:remove')")
    @Log(title = "客户档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dossierService.deleteDossierByIds(ids));
    }


    @PostMapping("/loadUser")
    public void regularlyAddDossier ()
    {
        String accessToken = tokenUtils.getAccessToken();
        List<OkrStaff> all = staffService.findAll();
        String urlFor = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token=ACCESS_TOKEN";

        String url = String.format(urlFor,accessToken);

        List<String> userIds = all.stream()
                .map(OkrStaff::getUserId)  // 获取每个元素的userId
                .collect(Collectors.toList());

        HashMap<String, Object> map = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userid_list", userIds);
//        HttpTool.sendPost(url)

    }

}
